package com.hue.gulimail.gulimailproduct.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hue.gulimail.gulimailproduct.entity.CategoryEntity;
import com.hue.gulimail.gulimailproduct.service.CategoryService;
import com.hue.common.utils.R;


/**
 * 商品三级分类
 *
 * @author yaofengqiao
 * @email 451737523@qq.com
 * @date 2021-08-12 16:24:48
 */
@RestController
@RequestMapping("gulimailproduct/category")
public class CategoryController{
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类(3级分类)，返回树形结构
     */
    @RequestMapping("/list")
    public R list() {
        List<CategoryEntity> listWithTree = categoryService.listWithTree();
        return R.ok().put("listWithTree", listWithTree);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        category.setSort(0);
        category.setShowStatus(1);
        category.setProductCount(0);
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateDetailById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.selectRemoveByIds(catIds);
        return R.ok();
    }

}
